package com.zl.learning.io.file;

import java.io.IOException;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.EnumSet;

public class TestFIleChannel {


    public static void main(String[] args) {
        Path path = Paths.get("D:\\Home\\Books\\TXT电子书\\世界名著", "安娜·卡列宁娜.txt");
        MappedByteBuffer buffer = null;
        try (FileChannel fileChannel = (FileChannel.open(path, EnumSet.of(StandardOpenOption.READ)))) {
//            buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
            buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        if (buffer != null) {
            try {
//                Charset charset = Charset.defaultCharset();
                Charset charset = Charset.forName("GBK");
                CharsetDecoder decoder = charset.newDecoder();
                CharBuffer charBuffer = decoder.decode(buffer);
                String content = charBuffer.toString();
                System.out.println(content);
                buffer.clear();
            } catch (CharacterCodingException ex) {
                ex.printStackTrace();
            }
        }
    }
}
